// mypages/sign/sign.js
Page({
    data: {
        formPhone: '',
        formpassword: ''
    },
    //获取输入的账号
    // getZhanghao(event) {
    // //console.log('账号', event.detail.value)
    // this.setData({
    //  zhanghao: event.detail.value
    // })

    // },
    //获取输入的密码
    // getMima(event) {
    // // console.log('密码', event.detail.value)
    // this.setData({
    //  mima: event.detail.value
    // })
    // },
    //点击跳转注册页面
    url: function () {
        wx.navigateTo({
            url: '/mypages/login/login',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //点击登陆
    sign() {
        let phone = this.data.formPhone
        let password = this.data.formpassword
        console.log('账号', phone, '密码', password)
        if (phone.length < 11) {
            wx.showToast({
                icon: 'none',
                title: '手机账号至少11位',
            })
            return
        }
        if (password.length < 4) {
            wx.showToast({
                icon: 'none',
                title: '密码至少4位',
            })
            return
        }

        //登陆
        wx.cloud.database().collection('my_user').where({
            phone: phone
        }).get({
            success(res) {
                console.log("获取数据成功", res)
                let user = res.data[0]
                console.log("user", user)
                if (!user) {
                    console.log('登陆失败')
                    wx.showToast({
                        icon: 'none',
                        title: '账号或密码不正确',
                    })
                    return
                }
                if (user.password && password == user.password) {
                    console.log('登陆成功')
                    wx.showToast({
                        title: '登陆成功',
                    })
                    setTimeout(function(){
                        wx.switchTab({
                            url: '/mypages/my/my',
                        })
                    },1000)
                    
                    //保存用户登陆状态
                    wx.setStorageSync('user', user)
                    return
                }
                
            },
            fail(res) {
                console.log("获取数据失败", res)
            }
        })
    }
})
